package com.xingfly.repository;

import com.xingfly.model.Follower;
import com.xingfly.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by SuperS on 2016/11/29.
 */
public interface FollowerRepository extends JpaRepository<Follower, Long> {
    //获取用户的粉丝
    List<Follower> findByUserId(Long id);

    //根据用户ID和关注ID 获取粉丝关系对象
    @Query("SELECT f FROM Follower f WHERE f.user.id=:userId AND f.followerUser.id=:followerUserId")
    Follower findByUserIdAndFollowerUserId(@Param("userId") Long userId, @Param("followerUserId") Long followerUserId);

    //使用JPQL 根据用户ID获取粉丝 数量
    @Query("SELECT COUNT(f) FROM Follower f WHERE f.user.id = :id")
    Long getFollowerCountByUserId(@Param("id") Long userId);

    //使用JPQL 根据用户ID获取粉丝列表
    @Query("SELECT f.followerUser FROM Follower f WHERE f.user.id = :id")
    List<User> getFollowersByUserId(@Param("id") Long userId);

    @Query("SELECT f.followerUser.id FROM Follower f WHERE f.user.id = :id")
    List<Long> getFollowerIdsByUserId(@Param("id") Long userId);

    @Query("SELECT f.followerUser.id FROM Follower f WHERE f.user.id = :id")
    Page<User> getFollowersByUserId(@Param("id") Long userId, Pageable Pageable);
}
